1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
/* eslint-disable max-statements */
import type { ParsedUrlQuery } from 'querystring';
import type { GetStaticPaths, GetStaticProps } from 'next';
import Head from 'next/head';
import { useRouter } from 'next/router';
import Script from 'next/script';
import { useCallback } from 'react';
import { useIntl } from 'react-intl';
import {
getLayout,
Heading,
LinksWidget,
PostsList,
Pagination,
type RenderPaginationLink,
type RenderPaginationItemAriaLabel,
Page,
PageHeader,
PageBody,
PageSidebar,
Spinner,
Notice,
LoadingPage,
} from '../../../components';
import {
convertWPThematicPreviewToPageLink,
convertWPTopicPreviewToPageLink,
fetchLastPostCursor,
fetchPostsCount,
fetchPostsList,
fetchThematicsCount,
fetchThematicsList,
fetchTopicsCount,
fetchTopicsList,
} from '../../../services/graphql';
import styles from '../../../styles/pages/blog.module.scss';
import type {
GraphQLConnection,
Maybe,
NextPageWithLayout,
Nullable,
WPPostPreview,
WPThematicPreview,
WPTopicPreview,
} from '../../../types';
import { CONFIG } from '../../../utils/config';
import { ROUTES } from '../../../utils/constants';
import {
getBlogSchema,
getLinksItemData,
getPostsWithUrl,
getSchemaJson,
getWebPageSchema,
} from '../../../utils/helpers';
import { loadTranslation, type Messages } from '../../../utils/helpers/server';
import {
useArticlesList,
useBreadcrumb,
useRedirection,
useThematicsList,
useTopicsList,
} from '../../../utils/hooks';
const renderPaginationLink: RenderPaginationLink = (pageNum) =>
`${ROUTES.BLOG}/page/${pageNum}`;
type BlogPageProps = {
data: {
posts: GraphQLConnection<WPPostPreview>;
thematics: GraphQLConnection<WPThematicPreview>;
topics: GraphQLConnection<WPTopicPreview>;
};
lastCursor: Maybe<Nullable<string>>;
pageNumber: number;
translation: Messages;
};
/**
* Blog index page.
*/
const BlogPage: NextPageWithLayout<BlogPageProps> = ({
data,
lastCursor,
pageNumber,
}) => {
useRedirection({
isReplacing: true,
to: ROUTES.BLOG,
whenPathMatches: (path) => path === `${ROUTES.BLOG}/page/1`,
});
const intl = useIntl();
const { isFallback } = useRouter();
const {
articles,
error,
firstNewResultIndex,
isLoading,
isLoadingMore,
isRefreshing,
hasNextPage,
loadMore,
} = useArticlesList({
after: lastCursor,
fallback: [data.posts],
perPage: CONFIG.postsPerPage,
});
const { isLoading: areThematicsLoading, thematics } = useThematicsList({
fallback: data.thematics,
input: { first: data.thematics.pageInfo.total },
});
const { isLoading: areTopicsLoading, topics } = useTopicsList({
fallback: data.topics,
input: { first: data.topics.pageInfo.total },
});
const messages = {
loading: {
thematicsList: intl.formatMessage({
defaultMessage: 'Thematics are loading...',
description: 'BlogPage: loading thematics message',
id: 'y37FuH',
}),
topicsList: intl.formatMessage({
defaultMessage: 'Topics are loading...',
description: 'BlogPage: loading topics message',
id: 'OsclKU',
}),
},
pageTitle: intl.formatMessage(
{
defaultMessage: 'Blog - Page {number}',
description: 'BlogPage: page title with number',
id: '8xVO3Y',
},
{
number: pageNumber,
}
),
pagination: {
noJS: intl.formatMessage({
defaultMessage:
"You can't load more articles without Javascript, please use the pagination instead.",
description: 'BlogPage: pagination no script message',
id: 'ZMES/E',
}),
title: intl.formatMessage({
defaultMessage: 'Pagination',
description: 'BlogPage: pagination accessible name',
id: 'AXe1Iz',
}),
},
seo: {
metaDesc: intl.formatMessage(
{
defaultMessage:
"Discover {websiteName}'s writings. He talks about web development, Linux and open source mostly.",
description: 'BlogPage: SEO - Meta description',
id: '18h/t0',
},
{ websiteName: CONFIG.name }
),
title: intl.formatMessage(
{
defaultMessage:
'Blog: development, open source - Page {number} - {websiteName}',
description: 'BlogPage: SEO - Page title',
id: 'dG3sT3',
},
{ number: pageNumber, websiteName: CONFIG.name }
),
},
widgets: {
thematicsListTitle: intl.formatMessage({
defaultMessage: 'Thematics',
description: 'BlogPage: thematics list widget title',
id: 'HriY57',
}),
topicsListTitle: intl.formatMessage({
defaultMessage: 'Topics',
description: 'BlogPage: topics list widget title',
id: '2D9tB5',
}),
},
};
const { items: breadcrumbItems, schema: breadcrumbSchema } = useBreadcrumb({
title: messages.pageTitle,
url: `${ROUTES.BLOG}/page/${pageNumber}`,
});
const webpageSchema = getWebPageSchema({
description: messages.seo.metaDesc,
locale: CONFIG.locales.defaultLocale,
slug: ROUTES.BLOG,
title: messages.pageTitle,
});
const blogSchema = getBlogSchema({
isSinglePage: false,
locale: CONFIG.locales.defaultLocale,
slug: ROUTES.BLOG,
});
const schemaJsonLd = getSchemaJson([webpageSchema, blogSchema]);
const renderPaginationLabel: RenderPaginationItemAriaLabel = useCallback(
({ kind, pageNumber: number, isCurrentPage }) => {
switch (kind) {
case 'backward':
return intl.formatMessage(
{
defaultMessage: 'Go to previous page, page {number}',
description: 'BlogPage: previous page label',
id: 'faO6BQ',
},
{ number }
);
case 'forward':
return intl.formatMessage(
{
defaultMessage: 'Go to next page, page {number}',
description: 'BlogPage: next page label',
id: 'oq3BzP',
},
{ number }
);
case 'number':
default:
return isCurrentPage
? intl.formatMessage(
{
defaultMessage: 'Current page, page {number}',
description: 'BlogPage: current page label',
id: 'JL6G22',
},
{ number }
)
: intl.formatMessage(
{
defaultMessage: 'Go to page {number}',
description: 'BlogPage: page number label',
id: 'IVczxR',
},
{ number }
);
}
},
[intl]
);
if (isFallback) return <LoadingPage />;
const pageUrl = `${CONFIG.url}${ROUTES.BLOG}`;
return (
<Page breadcrumbs={breadcrumbItems} isBodyLastChild>
<Head>
<title>{messages.seo.title}</title>
{/*eslint-disable-next-line react/jsx-no-literals -- Name allowed */}
<meta name="description" content={messages.seo.metaDesc} />
<meta property="og:url" content={pageUrl} />
{/*eslint-disable-next-line react/jsx-no-literals -- Content allowed */}
<meta property="og:type" content="website" />
<meta property="og:title" content={messages.pageTitle} />
<meta property="og:description" content={messages.seo.metaDesc} />
</Head>
<Script
// eslint-disable-next-line react/jsx-no-literals -- Id allowed
id="schema-blog"
type="application/ld+json"
// eslint-disable-next-line react/no-danger -- Necessary for schema
dangerouslySetInnerHTML={{ __html: JSON.stringify(schemaJsonLd) }}
/>
<Script
dangerouslySetInnerHTML={{ __html: JSON.stringify(breadcrumbSchema) }}
// eslint-disable-next-line react/jsx-no-literals -- Id allowed
id="schema-breadcrumb"
type="application/ld+json"
/>
<PageHeader
heading={messages.pageTitle}
meta={{ total: data.posts.pageInfo.total }}
/>
<PageBody>
{articles ? (
<PostsList
className={styles['posts-list']}
firstNewResult={firstNewResultIndex}
isLoading={isLoading || isLoadingMore || isRefreshing}
onLoadMore={hasNextPage ? loadMore : undefined}
posts={getPostsWithUrl(
articles.flatMap((page) => page.edges.map((edge) => edge.node))
)}
sortByYear
total={data.posts.pageInfo.total}
/>
) : null}
{error ? (
<Notice
// eslint-disable-next-line react/jsx-no-literals -- Kind allowed
kind="error"
>
{intl.formatMessage({
defaultMessage: 'Failed to load.',
description: 'BlogPage: failed to load text',
id: 'C/XGkH',
})}
</Notice>
) : null}
<noscript>
<Notice
// eslint-disable-next-line react/jsx-no-literals
kind="info"
>
{messages.pagination.noJS}
</Notice>
<Pagination
aria-label={messages.pagination.title}
className={styles.pagination}
current={pageNumber}
isCentered
renderItemAriaLabel={renderPaginationLabel}
renderLink={renderPaginationLink}
total={data.posts.pageInfo.total}
/>
</noscript>
</PageBody>
<PageSidebar>
{areThematicsLoading ? (
<Spinner>{messages.loading.thematicsList}</Spinner>
) : (
<LinksWidget
heading={
<Heading level={2}>{messages.widgets.thematicsListTitle}</Heading>
}
items={getLinksItemData(
thematics.edges.map((edge) =>
convertWPThematicPreviewToPageLink(edge.node)
)
)}
/>
)}
{areTopicsLoading ? (
<Spinner>{messages.loading.topicsList}</Spinner>
) : (
<LinksWidget
heading={
<Heading level={2}>{messages.widgets.topicsListTitle}</Heading>
}
items={getLinksItemData(
topics.edges.map((edge) =>
convertWPTopicPreviewToPageLink(edge.node)
)
)}
/>
)}
</PageSidebar>
</Page>
);
};
BlogPage.getLayout = (page) => getLayout(page);
type BlogPageParams = {
number: string;
} & ParsedUrlQuery;
export const getStaticProps: GetStaticProps<BlogPageProps> = async ({
locale,
params,
}) => {
const pageNumber = Number((params as BlogPageParams).number);
if (pageNumber === 1)
return {
redirect: {
destination: ROUTES.BLOG,
permanent: true,
},
};
const lastCursor =
pageNumber > 1
? await fetchLastPostCursor(CONFIG.postsPerPage * (pageNumber - 1))
: null;
const posts = await fetchPostsList({
first: CONFIG.postsPerPage,
after: lastCursor,
});
const totalThematics = await fetchThematicsCount();
const thematics = await fetchThematicsList({ first: totalThematics });
const totalTopics = await fetchTopicsCount();
const topics = await fetchTopicsList({ first: totalTopics });
const translation = await loadTranslation(locale);
return {
props: {
data: {
posts: JSON.parse(JSON.stringify(posts)),
thematics,
topics,
},
lastCursor,
pageNumber,
translation,
},
};
};
export const getStaticPaths: GetStaticPaths = async () => {
const totalArticles = await fetchPostsCount();
const totalPages = Math.ceil(totalArticles / CONFIG.postsPerPage);
const pagesArray = Array.from(
{ length: totalPages },
(_, index) => index + 1
);
const paths = pagesArray.map((number) => {
return { params: { number: `${number}` } };
});
return {
paths,
fallback: true,
};
};
export default BlogPage;
|